﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using System.Xml.Linq;
using MediaScraper.Dashboard.Infrastructure;
using MediaScraper.Dashboard.Models;
using MediaScraper.DataAccess;
using MediaScraper.WindowsService;

namespace MediaScraper.Dashboard.Controllers
{
    public class ConfigurationController : BaseController
    {
        public ActionResult Index()
        {
            var dashboardModel = new DashboardModel { ServiceRunning = ScraperController.IsRunning() };
            return View(dashboardModel);
        }

        public ActionResult AgentConfig()
        {
            var model = new AgentConfigModel();
            var doc = XDocument.Load(Server.MapPath("Config/agents.xml"));
            if (doc.Root != null)
            {
                var elements = doc.Root.Elements().Where(x => (string) x.Attribute("id") == "IMediaAgent");
                foreach (var xElement in elements)
                {
                    model.Agents.Add(new AgentModel() {Name = (string) xElement.Attribute("type")});
                }
            }
            //ToDo: Set "selected" depending wether this is set in DB

            return PartialView("_AgentConfig", model);
        }

        public ActionResult AdapterConfig()
        {
            var model = new AdapterConfigModel();
            using (var context = new DataContext())
            {
                var appConfig = context.ApplicationConfigurations.FirstOrDefault();
                model.SelectedAdapter = new AdapterModel()
                    {
                        DisplayName = appConfig.AdapterName,
                        FullName = appConfig.AdapterName
                    };
                model.Adapters = new List<SelectListItem>()
                    {
                        new SelectListItem() {Text = "MediaScraper.Adapters.Plex.PlexAdapter", Value = "MediaScraper.Adapters.Plex.PlexAdapter"},
                        new SelectListItem() {Text = "MediaScraper.Adapters.Demo.DemoAdapter", Value = "MediaScraper.Adapters.Demo.DemoAdapter"},
                    };
            }
            return PartialView("_AdapterConfig", model);
        }


        [System.Web.Http.HttpPost]
        public ActionResult UpdateAdapterConfig(AdapterConfigModel model)
        {
            using (var context = new DataContext())
            {
                var appConfig = context.ApplicationConfigurations.FirstOrDefault();
                appConfig.AdapterName = model.SelectedAdapter.FullName;
                context.SaveChanges();
            }
            return RedirectToAction("AdapterConfig");
        }

        public ActionResult FolderConfig()
        {
            var model = new FolderConfigModel();
            using (var context = new DataContext())
            {
                var appConfig = context.ApplicationConfigurations.FirstOrDefault();
                model.DownloadFolder = appConfig.DownloadRootPath;

            }

            return PartialView("_FolderConfig", model);
        }

        [System.Web.Http.HttpPost]
        public ActionResult UpdateFolderConfig(FolderConfigModel model)
        {
            var exists = Directory.Exists(model.DownloadFolder);
            if (exists)
            {
                using (var context = new DataContext())
                {
                    var appConfig = context.ApplicationConfigurations.FirstOrDefault();
                    appConfig.DownloadRootPath = model.DownloadFolder;
                    context.SaveChanges();
                }
            }
            else
            {
                ModelState.AddModelError("DownloadFolder", "Folder does not exist.");
                return PartialView("_FolderConfig", model);
            }

            return RedirectToAction("FolderConfig");
        }



    }
}
